
/*头文件*/
//------------------------------------
#include "user.h"
//------------------------------------

/*使用文件，文件指针*/
//------------------------------------
extern char* user_info;
extern char* lottery_info;
extern char* lottery_man;
extern char* prize_pool;
extern FILE* f_prize_pool, *f_lottery_man;
//------------------------------------

/*全局变量声明*/
//------------------------------------
extern int QIHAO, perPrice_lottery, STATE,PRIZE_POOL;
//------------------------------------

/* 函数功能：界面
 * 形参：用户名
 * 返回：
 */
void menu(char* user)
{
	/*系统清屏*/
	system("clear"); 
	/*欢迎界面*/
	printf("欢迎用户 %s 登陆!\n",user);
	printf("-----------------------------------------\n");
	printf("1.用户信息查询\n"); //查找user_info
	printf("2.购买彩票\n");
	printf("3.充值\n");
	printf("4.提现\n");
	printf("5.票务查询\n"); //查找lottery_info
	printf("6.修改密码\n"); //操作manage
	printf("7.注销帐号\n");
	printf("0.保存并登出\n");
	printf("11.显示菜单\n");
	printf("99.退出系统\n");
	printf("-----------------------------------------\n");
	return;
}

/* 函数功能：显示自身信息，包含用户名和余额
 * 形参：用户结构体
 * 返回：
 */
void info_user(User* pusers)
{
		printf("姓名：%s 余额：%d\n",pusers->name,pusers->balance);
		return;
}

/* 函数功能：显示彩票历史记录
 * 形参：用户结构体，彩票结构体
 * 返回：
 */
void info_lotteries(User* pusers, xLottery* plots)
{
	printf("-----------------------------------------\n");
	FILE* f_lot_info=fopen(lottery_info,"r");
	if(!f_lot_info){perror("");exit(1);}
	printf("ID\t帐号\t号码\t期号\t\t开奖状态\t中奖状态\t获得奖金\n");
	while(fscanf(f_lot_info,"%d %s %s %d %d %d %d",\
	&plots->id,plots->name,plots->lottery_num,\
	&plots->issue_version,&plots->issue_status,\
	&plots->prize_status,&plots->prize_gain)>0)
	{
		if(!strcmp(pusers->name, plots->name))
		printf("%d\t%s\t%s\t%d\t\t%d\t\t%d\t\t%d\n",\
		plots->id,plots->name,plots->lottery_num,\
		plots->issue_version,plots->issue_status,\
		plots->prize_status,plots->prize_gain);
	}
	fclose(f_lot_info);
	printf("-----------------------------------------\n");
}

/* 函数功能：充值
 * 形参：用户结构体
 * 返回：
 */
void recharge(User* pusers)
{
	int rech_money=0; 
	while(1){
	printf("请输入充值金额(按q退出)：");
	if(1!=scanf("%d",&rech_money))if(getchar()=='q')return;
	if(rech_money>=0)break;}
	pusers->balance +=rech_money;
	printf("充值成功！\n");
	printf("姓名：%s  余额：%d\n",pusers->name,pusers->balance);
	
	return;
}

/* 函数功能：提现
 * 形参：用户结构体
 * 返回：
 */
void withdraw(User* pusers)
{
	int withdraw_money=0; 
	while(1){
		printf("请输入提现金额(按q退出)：");
		if(1!=scanf("%d",&withdraw_money))if(getchar()=='q')return;
		while(getchar()!='\n');
		if(withdraw_money>=0 && withdraw_money<=pusers->balance)break;
		else printf("无法提现！请输入金额在余额%d范围内！\n",pusers->balance);
	}
	pusers->balance -= withdraw_money;
	printf("提现成功！\n");
	printf("姓名：%s  余额：%d\n",pusers->name,pusers->balance);
	
	return;
}


/* 函数功能：购买彩票
 * 形参：用户结构体
 * 返回：
 */
void buy(User* pusers)
{
	if(STATE){printf("当前尚未开票！\n");return;}
	
	if(perPrice_lottery > pusers->balance){printf("超出余额！请充值！\n");return;}
	
	printf("温馨提示：小赌宜情，大赌伤身！\n");
	printf("彩票单价:%d, 本次最多%d注,每注一个数字(范围0~9)！\n",\
	perPrice_lottery,LOTTERY_SIZE);
	
	FILE* f_buy_lot=fopen(lottery_info,"a");
	if(!f_buy_lot){perror("");exit(1);}
	int count=0,nid=0; char lot_num[LOTTERY_SIZE]; 
	
	while(1){ //单次购买最多LOTTERY_SIZE个
		printf("请输入彩票号码第%d位数字(q退出)：",count+1);
		scanf("%c",&lot_num[count]); while(getchar()!='\n');
		
		if(count<3&&lot_num[count]=='q')
		{
			printf("至少下注3位！确定退出购票吗？(按q退出,任意键继续)\n");
			if(getchar()=='q')return;
			else continue;
		}
		if(lot_num[count]=='q')break;
		if(lot_num[count]<'0'||lot_num[count]>'9')
		{printf("格式不正确！\n");continue;}

		++count;
		if(count==LOTTERY_SIZE)break;
	}
	
	lot_num[count]='\0';
	
	
	printf("购买成功！\n");
	pusers->balance -= perPrice_lottery;
	
	/*修改奖金池*/
	//------------------------------------------------------------
	f_prize_pool=fopen(prize_pool,"r");
	if(!f_prize_pool){perror("");exit(1);}
	fscanf(f_prize_pool,"%d",&PRIZE_POOL);
	fclose(f_prize_pool);
	
	PRIZE_POOL += perPrice_lottery;
	
	f_prize_pool=fopen(prize_pool,"w");
	if(!f_prize_pool){perror("");exit(1);}
	fprintf(f_prize_pool,"%d\n",PRIZE_POOL);
	fclose(f_prize_pool);
	//------------------------------------------------------------
	
	fprintf(f_buy_lot, "%d %s %s %d %d %d %d\n", \
	(nid=ftell(f_buy_lot)/5), pusers->name,lot_num, QIHAO,0,0,0);
	
	printf("ID:%d 姓名:%s 号码:%s 期号:%d 开奖状态:%d 得奖状态:%d 获得金额:%d\n",nid, pusers->name,lot_num, QIHAO,0,0,0);

	printf("姓名：%s  余额：%d\n",pusers->name,pusers->balance);
	fclose(f_buy_lot);
	return;
}

/* 函数功能：修改密码
 * 形参：用户结构体
 * 返回：
 */
void pwdAlter(User* pusers)
{
	char ori_pwd[CODE_SIZE], new_pwd[CODE_SIZE];
	
	while(1){
	printf("请确认原密码："); scanf("%s",ori_pwd);
	if(strcmp(pusers->pwd, ori_pwd)){printf("输入错误！\n");continue;}
	break;}
	while(1){
		while(getchar()!='\n');
		printf("请输入新密码：\n"); get_code(new_pwd);
		int len=strlen(new_pwd);if(!(len>1 && len<CODE_SIZE))
		{printf("输入错误！\n");continue;}
		printf("请再次输入新密码：\n"); get_code(ori_pwd);
		if(strcmp(new_pwd, ori_pwd)){printf("输入错误！\n");continue;}
		break;
	}
	strcpy(pusers->pwd, ori_pwd);
	printf("姓名：%s 密码：%s\n",pusers->name,pusers->pwd);
	printf("修改成功！\n");
}

/* 函数功能：保存功能
 * 形参：用户结构体，文件指针，注销标志
 * 返回：
 */
void save_user(User* pusers, FILE* f_user, FILE* ptemp_user,int notDele)
{
	if(notDele)fwrite(pusers,sizeof(User),1,ptemp_user);
	while(fscanf(f_user,"%s %s %d",pusers->name,pusers->pwd,&pusers->balance)>0)
	fwrite(pusers,sizeof(User),1,ptemp_user);
	
	fclose(f_user); rewind(ptemp_user);
	
	FILE* fp=fopen(user_info,"w");
	if(!fp){perror("");exit(1);}
	while(fread(pusers,sizeof(User),1,ptemp_user))
	fprintf(fp,"%s %s %d\n",pusers->name,pusers->pwd,pusers->balance);
	
	fclose(fp);
}

/* 函数功能：注销
 * 形参：用户结构体，彩票结构体
 * 返回：who
 */
void cancel_lots(User* pusers, xLottery* plots)
{
	FILE* lot_temp=tmpfile();
	if(!lot_temp){perror("");exit(1);}
	FILE* f_lot_info=fopen(lottery_info,"r");
	if(!f_lot_info){perror("");exit(1);}
	while(fscanf(f_lot_info,"%d %s %s %d %d %d %d",\
	&plots->id,plots->name,plots->lottery_num,\
	&plots->issue_version,&plots->issue_status,\
	&plots->prize_status,&plots->prize_gain)>0)
	{	
		if(!strcmp(pusers->name, plots->name))continue;
		fwrite(plots,sizeof(xLottery),1,lot_temp);
	}
	fclose(f_lot_info); rewind(lot_temp);
	
	FILE* f_lot_renew=fopen(lottery_info,"w");
	if(!f_lot_renew){perror("");exit(1);}
	while(fread(plots,sizeof(xLottery),1,lot_temp))
		fprintf(f_lot_renew,"%ld %s %s %d %d %d %d\n",\
	ftell(f_lot_renew),plots->name,plots->lottery_num,\
	plots->issue_version,plots->issue_status,\
	plots->prize_status,plots->prize_gain);

	fclose(f_lot_renew);
}

/* 函数名：获取密码(隐藏显示)
 * 形参：密码字符串指针
 * 返回：
 */
void get_code(char* c) 
{
	while(1){
	system("stty -icanon"); //stty -icanon 设置一次性读完操作，如使用getchar()读操作，不需要按enter
	system("stty -echo"); //stty -echo 设置命令不被显示
	printf("输入密码(不大于%d个字符):",CODE_SIZE-1);
	int i=0;
	while(1){
		char ch;
		ch = getchar();
		if((ch  == 127 || ch  == 8) && i>0)//当有键入时，按键delete或backspace时，删除一位
			{
				printf("\b");	//这里是删除一个，通过输出回撤符 /b，回撤一格，
				printf(" ");	//再显示空格符把刚才的*给盖住，
				printf("\b");	//然后再 回撤一格等待录入。	
				--i; //此时i指在空格处
			}
		else if(ch==10 || ch == 13)//当键入的为换行时
			{
				c[i] = '\0';//将字符串以'\0'结尾
				break;
			}
		else{c[i]=ch;printf("*");++i;} //成功读取时
	}
	system("stty echo"); // stty echo 取消不显设置
	system("stty icanon"); //stty icanon 取消设置
	printf("\n");
	if(i>0 && i<CODE_SIZE)break;
	}
	return;
}
